package home.project.misc;

import flash.display.Graphics;
import openfl.geom.Point;

/**
 * ...
 * @author an.vuongngocduy
 */
class Camera extends Entity
{
	public static var current(default, null):Camera;
	
	private var __target:Entity;
	
	public function new(p_width:Float, p_height:Float) {
		super();
		__boundingBoxes.push(new Rect(this, 0, 0, p_width, p_height));
		
		if (current == null) current = this;
	}
	
	public function contains(p_entity:Entity):Bool {
		var r:Rect = boundingBoxes.next();
		for (bound in p_entity.boundingBoxes) {
			if (r.intersects(bound)) {
				return true;
			}
		}
		return false;
	}
	
	public override function draw(p_g:Graphics):Void {
		p_g.lineStyle(3.0, 0x00FFFF, 1, true);
		var r:Rect = boundingBoxes.next();
		p_g.moveTo(r.left, r.top);
		p_g.lineTo(r.right, r.top);
		
		p_g.moveTo(r.right, r.top);
		p_g.lineTo(r.right, r.bottom);
		
		p_g.moveTo(r.right, r.bottom);
		p_g.lineTo(r.left, r.bottom);
		
		p_g.moveTo(r.left, r.bottom);
		p_g.lineTo(r.left, r.top);
	}
	
	public function follow(p_entity:Entity):Void {
		__target = p_entity;
	}
	
	public override function update(p_dt:Float) {
		if (__target != null) {
			position = new Point(__target.absoluteTransform.tx , __target.absoluteTransform.ty);
		}
	}
}